/**
 * jQuery Countdown plugin
 * Depends on moment.js to work!
 * Created by Igor Ramadas for http://counting.to
 */
(function($)
{
    $.fn.countdown = function(options)
    {
        var config = {};
        $.extend(config, options);

        var totalSeconds = this.setCountdown(config);

        if (config.callback)
        {
            $.data($(this)[0], "callback", config.callback);
        }

        var newHtml = '<div class="top"></div><div class="bottom"></div>';
        $("#" + $(this).attr("id") + " .digit").html(newHtml);

        $(this).doCountdown($(this).attr("id"), totalSeconds);

        return this;
    };

    $.fn.stopCountdown = function()
    {
        clearTimeout($.data(this[0], "timer"));
    };

    $.fn.startCountdown = function()
    {
        this.doCountdown($(this).attr("id"), $.data(this[0], "totalSeconds"));
    };

    $.fn.setCountdown = function(options)
    {
        var $el = $(this[0]);
        var totalSeconds = $el.data("totalSeconds");
        var targetDate = $el.data("value");

        if (!totalSeconds && targetDate)
        {
            totalSeconds = moment(targetDate).toDate().getTime() - moment().toDate().getTime();
            totalSeconds = totalSeconds / 1000;

            $el.data("totalSeconds", totalSeconds);
        }

        return totalSeconds;
    };

    $.fn.doCountdown = function(id, totalSeconds)
    {
        var $this = $("#" + id);

        if (totalSeconds <= 0)
        {
            totalSeconds = 0;

            if ($.data($this[0], "timer"))
            {
                clearTimeout($.data($this[0], "timer"));
            }
        }

        var secs = Math.floor(totalSeconds % 60);
        var mins = Math.floor(totalSeconds / 60) % 60;
        var hours = Math.floor(totalSeconds / 60 / 60) % 24;
        var days = Math.floor(totalSeconds / 60 / 60 / 24);
        var months = Math.floor(totalSeconds / 60 / 60 / 24 / 30);
        var years = Math.floor(totalSeconds / 60 / 60 / 24 / 30);

        $this.dashChangeTo(id, "seconds", secs);
        $this.dashChangeTo(id, "minutes", mins);
        $this.dashChangeTo(id, "hours", hours);
        $this.dashChangeTo(id, "days", days);
        $this.dashChangeTo(id, "months", months);
        $this.dashChangeTo(id, "years", years);

        $.data($this[0], "totalSeconds", totalSeconds);

        if (totalSeconds > 0)
        {
            var e = $this;
            var t = setTimeout(function()
            {
                e.doCountdown(id, totalSeconds - 1)
            }, 1000);

            $.data(e[0], "timer", t);
        }
        else if (cb = $.data($this[0], "callback"))
        {
            $.data($this[0], "callback")();
        }
    };

    $.fn.dashChangeTo = function(id, dash, n)
    {
        var $this = $("#" + id);

        for (var i = ($this.find("." + dash + " .digit").length - 1); i >= 0; i--)
        {
            var d = n % 10;
            n = (n - d) / 10;
            $this.digitChangeTo("#" + $this.attr("id") + " ." + dash + " .digit:eq(" + i + ")", d);
        }
    };

    $.fn.digitChangeTo = function(digit, n)
    {
        var duration = 160;

        if ($(digit + " div.top").html() != n + "")
        {
            $(digit + " div.top").css({display: "none"});
            $(digit + " div.top").html((n ? n : "0")).slideDown(duration);

            $(digit + " div.bottom").animate({height: ""}, duration, function()
            {
                $(digit + " div.bottom").html($(digit + " div.top").html());
                $(digit + " div.bottom").css({display: "block", height: ""});
                $(digit + " div.top").hide();
            });
        }
    };
})(jQuery);